package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.LPaper;
import com.education.welco.pojo.LPaperQueRelation;
import com.education.welco.pojo.LQuestions;
import com.education.welco.service.ILPaperQueRelationService;
import com.education.welco.service.ILPaperService;
import com.education.welco.service.ILQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 试卷试题 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/lQuestions")
public class LQuestionsController extends BaseController {

    @Autowired
    private ILQuestionsService questionsService;
    @Autowired
    private ILPaperQueRelationService paperQueRelationService;
    @Autowired
    private ILPaperService paperService;


    /**
     * 查询试题列表  根据课程id 在中间表中查询出试题id
     */
    @GetMapping("/list")
    public ApiReturnObject list(Integer catalogId, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        if(catalogId != null){
            LQuestions questions = new LQuestions();
            questions.setCatalogId(catalogId);
            List<LQuestions> lQuestions = questionsService.selectLQuestionsListOther(questions);
//          PageInfo<LQuestions> pageInfo = new PageInfo<>(lQuestions);
            return new ApiReturnObject("200","查询成功",lQuestions);
        }
        return new ApiReturnObject("201","缺少catalogId","");
    }


    /**
     * 新增试题
     */
    @PostMapping("/addAll")
    public ApiReturnObject addSave(@RequestBody LQuestions questions) {
        if(questions.getCatalogId() != null){
            questions.setCreateTime(new Date());
            questionsService.insertLQuestions(questions);
            //取到新添加试题id  为中间表添加数据
            Integer id = questions.getId();
            //新建中间表对象
            LPaperQueRelation sCatalogQuestions = new LPaperQueRelation();
            //设置值
            sCatalogQuestions.setPaperId(questions.getPaperId());
            sCatalogQuestions.setQuestionId(id);
            //添加数据
            paperQueRelationService.insertLPaperQueRelation(sCatalogQuestions);
            return new ApiReturnObject("200","添加成功","");
        }
        return new ApiReturnObject("201","缺少catalogId","");

    }


    /**
     * 新增试卷 以及 试题
     */
    @PostMapping("/add")
    public ApiReturnObject addAll(@RequestBody LQuestions questions) {
        if(questions.getCatalogId() != null){
            //新增试卷
            Integer i ;
            if(questions.getPaperName() != null && !"".equals(questions.getPaperName())){
                LPaper lPaper = new LPaper();
                lPaper.setCreateTime(new Date());
                lPaper.setCatalogId(questions.getCatalogId());
                lPaper.setSjIntroduction(questions.getPaperName());
                paperService.insertLPaper(lPaper);
                i = lPaper.getId();
            }else {
                return new ApiReturnObject("201","请填写试卷名称","");
            }
            //新增试题
            if(questions.getQuestionsList() != null && questions.getQuestionsList().size() > 0){
                for (LQuestions lQuestions : questions.getQuestionsList()) {
                    lQuestions.setCreateTime(new Date());
                    questionsService.insertLQuestions(lQuestions);
                    //取到新添加试题id  为中间表添加数据
                    Integer id = lQuestions.getId();
                    //新建中间表对象
                    LPaperQueRelation sCatalogQuestions = new LPaperQueRelation();
                    //设置值
                    sCatalogQuestions.setPaperId(i);
                    sCatalogQuestions.setQuestionId(id);
                    //添加数据
                    paperQueRelationService.insertLPaperQueRelation(sCatalogQuestions);
                }
            }
            return new ApiReturnObject("200","添加成功","");
        }
        return new ApiReturnObject("201","缺少catalogId","");

    }
    
    
    @GetMapping("selectOne")
    public ApiReturnObject selectOne(Integer id){
        if(id != null){
            HashMap<String, Object> map = new HashMap<>(3);
            LPaper paper = paperService.selectLPaperById(id);
            String paperName = paper.getSjIntroduction();
            map.put("paperName",paperName);
            map.put("questionsList",new ArrayList<LQuestions>());
            if(paper != null){
                List<LQuestions> questionsList = questionsService.selectLQuestionsListOthers(paper.getId());
                map.put("questionsList",questionsList);
            }
            return new ApiReturnObject("200","查询成功",map);
        }
        return new ApiReturnObject("201","缺少参数id","");
    }


    /**
     * 修改试题
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody LQuestions questions) {
        if(questions.getId() != null){
            questionsService.updateLQuestions(questions);
            return new ApiReturnObject("200","修改成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }

    /**
     * 删除试题
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody LQuestions questions) {
        if(questions.getId() != null){
//            questionsService.deleteLQuestionsById(questions.getId());
            //删除试题
            questionsService.deleteLQuestionsById2(questions.getId());
            //删除试卷
            paperService.deleteLPaperById(questions.getId());
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少id","");
    }

}